<?php

class searchActions extends sfActions {

    public function preExecute(){
        if (!$this->getUser()->hasAttribute('scountry')){
            if ($this->getUser()->isFirstRequest()){
                $culture = $this->getRequest()->getPreferredCulture(array('en', 'es'));
                $this->getUser()->setCulture($culture);
                $this->getUser()->isFirstRequest(false);
                $country = CountryPeer::retrieveByPK(169);/* 169: ID Perú en BD */
                $this->getUser()->setAttribute('scountry',$country);
            }else{
                $culture = $this->getUser()->getCulture();
            }
        }
    }

    public function executeIndex(sfWebRequest $request){
        $title = $request->getParameter('title');
        $date = $request->getParameter('date');
        $date2 = $request->getParameter('date2');
        $star = $request->getParameter('estrellas');
        $author = $request->getParameter('author');
        $category = $request->getParameter('category');
        $subcategory = $request->getParameter('subcategory');
        $c = new Criteria();
        if($title!=''){
            $c->add(ArticlePeer::TITLE, '%'.$title.'%', Criteria::LIKE);
        }
        $this->getUser()->setAttribute('sparam_title',$title);

        if($date!='' && $date2!=''){
            $criterion = $c->getNewCriterion(ArticlePeer::CREATED_AT, $date, Criteria::GREATER_EQUAL);
            $criterion2 = $c->getNewCriterion(ArticlePeer::CREATED_AT, $date2, Criteria::LESS_EQUAL);

            $criterion->addAnd($criterion2);
            $c->add($criterion);
        }elseif($date!=''){
            $criterion = $c->getNewCriterion(ArticlePeer::CREATED_AT, $date, Criteria::GREATER_EQUAL);
            $c->add($criterion);
        }elseif($date2!=''){
            $criterion = $c->getNewCriterion(ArticlePeer::CREATED_AT, $date2, Criteria::LESS_EQUAL);
            $c->add($criterion);
        }
        $this->getUser()->setAttribute('sparam_date',$date);
        $this->getUser()->setAttribute('sparam_date2',$date2);

        if($star!='' && $star!=0){
            $criterion = $c->getNewCriterion(ArticlePeer::SCORE, intval($star), Criteria::GREATER_EQUAL);
            $criterion2 = $c->getNewCriterion(ArticlePeer::SCORE, intval($star)+1, Criteria::LESS_EQUAL);

            $criterion->addAnd($criterion2);
            $c->add($criterion);
            $this->getUser()->setAttribute('sparam_star',intval($star));
        }

        if($author!=''){
            $array_users_by_name_ids = array();

            $myLib = new myLib();
            $user_ids = $myLib->getUserIdsFromPractilByName($author);

            $c->add(AccountPeer::PRACTIL_ACCOUNT_ID, $user_ids, Criteria::IN);
            $c->addJoin(ArticlePeer::OWNER_ID, AccountPeer::ID);
        }
        $this->getUser()->setAttribute('sparam_author',$author);

        if($category!=''){
            $c->add(ArticlePeer::CATEGORY_ID, $category);
        }
        $this->getUser()->setAttribute('sparam_category',$category);

        if($subcategory!=''){
            $c->add(ArticlePeer::SUBCATEGORY_ID, $subcategory);
        }
        $this->getUser()->setAttribute('sparam_subcategory',$subcategory);

        $c->add(ArticlePeer::ENABLED, 1);
        $c->add(ArticlePeer::IS_DRAFT, 0);
        $articles = ArticlePeer::doSelect($c);
        $this->articles = $articles;

        $c->clear();
        $c->add(CountryPeer::ID, $this->getUser()->getAttribute('scountry')->getId());
        $c->add(DetCountryCategoryPeer::IS_SHOWN, 1);
        $c->addJoin(DetCountryCategoryPeer::CATEGORY_ID, CategoryPeer::ID);
        $c->addJoin(DetCountryCategoryPeer::COUNTRY_ID, CountryPeer::ID);
        $c->addDescendingOrderByColumn(CategoryPeer::ID);
        $this->categories = CategoryPeer::doSelectWithI18n($c);

        if($this->getUser()->hasAttribute('sparam_category')){
            $c->clear();
            $c->add(CountryPeer::ID, $this->getUser()->getAttribute('scountry')->getId());
            $c->add(SubcategoryPeer::CATEGORY_ID, $this->getUser()->getAttribute('sparam_category'));
            $c->add(DetCountrySubcategoryPeer::IS_SHOWN, 1);
            $c->addJoin(DetCountrySubcategoryPeer::SUBCATEGORY_ID, SubcategoryPeer::ID);
            $c->addJoin(DetCountrySubcategoryPeer::COUNTRY_ID, CountryPeer::ID);
            $c->addDescendingOrderByColumn(SubcategoryPeer::ID);
            $this->subcategories = SubcategoryPeer::doSelectWithI18n($c);
        }

        $array_users = array();
        if(count($articles)>0){
            $ids = array();
            foreach($articles as $article){
                $ids[] = $article->getAccount()->getPractilAccountId();
            }
            /* crear metodo que reciba id y devuelva al usuario practil */
            $lib = new myPractil();
            $url = $lib->url_practil_get_users($ids);

            $respuesta_get_users = @file_get_contents($url);
            if($respuesta_get_users === FALSE){
                $this->getUser()->setFlash('message','Not connected to Practil');
            }else{
                $json_get_users = json_decode($respuesta_get_users);
                if($json_get_users->{'success'}==false){
                    $this->getUser()->setFlash('message',$json_get_users->{'message'});
                }else{
                    $array_users = $json_get_users->{'practil'}->{'accounts'};
                    if(count($array_users)<=0){
                        $this->getUser()->setFlash('message','Users Array returned 0 elements');
                    }
                }
            }
        }
        $this->array_users = $array_users;
    }

    public function executeClear(sfWebRequest $request){
        $this->getUser()->getAttributeHolder()->remove('sparam_title');
        $this->getUser()->getAttributeHolder()->remove('sparam_date');
        $this->getUser()->getAttributeHolder()->remove('sparam_date2');
        $this->getUser()->getAttributeHolder()->remove('sparam_star');
        $this->getUser()->getAttributeHolder()->remove('sparam_author');

        $this->redirect('@search');
    }

}